const router = require('koa-router')();
const studentService = require('../service/studentService.js');
const result = require("../com/result.js")
const log = require('../com/logConfig.js');
router.prefix('/student')

/**
 * 根据学生ID来得到学生人物关系数据
 */
router.get('/getAll', async (ctx, next) => {
    try {
        let res = studentService.getAll(ctx, next);
        return await res;
    } catch (e) {
        log.error('获取学生人物关系异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 根据学校id(可选)和学生id查学生最新位置数据
 */
router.post('/getStudentLocation', async (ctx, next) => {
    try {
        let res = studentService.getStudentLocation(ctx, next);
        return await res;
    } catch (e) {
        log.error('获取学生位置异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 查学生位置信息 - 过滤空Gps - 每个学生只查最近的一条
 */
router.post('/getLocationNNEmpty', async (ctx, next) => {
    try {
        let res = studentService.getLocationNNEmpty(ctx, next);
        return await res;
    } catch (e) {
        log.error('查学生有Gps位置信息异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 根据学校id(可选)和学生id查学生一段时间的轨迹
 */
router.get('/getStuOrbit', async (ctx, next) => {
    try {
        let res = studentService.getStuOrbit(ctx, next);
        return await res;
    } catch (e) {
        log.error('获取学生轨迹异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 校徽报警/报警柱报警/火灾报警 - 查离学生最近的相关人员安排任务 - 查离报警地点最近的保安
 */
router.post('/getSosCandidate', async (ctx, next) => {
    try {
        let res = studentService.getSosCandidate(ctx, next);
        return await res;
    } catch (e) {
        log.error('查离报警地点最近的相关人员异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 推送消息给家长阶段保存流程图和人物关系细节数据 - 以后所有的报警记录就可以自由的追溯
 */
router.post('/saveProcess', async (ctx, next) => {
    try {
        let res = studentService.saveProcess(ctx, next);
        return await res;
    } catch (e) {
        log.error('推送消息给家长阶段保存流程图和人物关系细节数据异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 分配任务给保安作为候选人阶段保存流程图和人物关系细节数据 - 以后所有的报警记录就可以自由的追溯
 */
router.post('/saveProcessAtAssign', async (ctx, next) => {
    try {
        let res = studentService.saveProcessAtAssign(ctx, next);
        return await res;
    } catch (e) {
        log.error('分配任务给保安作为候选人阶段保存流程图和人物关系细节数据异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 某一个保安已经到达报警地点阶段保存流程图和人物关系细节数据 - 以后所有的报警记录就可以自由的追溯
 */
router.post('/savaProcessAtReach', async (ctx, next) => {
    try {
        let res = studentService.savaProcessAtReach(ctx, next);
        return await res;
    } catch (e) {
        log.error('某一个保安已经到达报警地点阶段保存流程图和人物关系细节数据异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 保安处理完报警保存流程图和人物关系细节数据 - 以后所有的报警记录就可以自由的追溯
 */
router.post('/savaProcessAtOver', async (ctx, next) => {
    try {
        let res = studentService.savaProcessAtOver(ctx, next);
        return await res;
    } catch (e) {
        log.error('保安处理完报警保存流程图和人物关系细节数据异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 校徽报警 - 存某个组织的队长分配任务数据细节
 */
router.post('/savaTissueLeader', async (ctx, next) => {
    try {
        let res = studentService.savaTissueLeader(ctx, next);
        return await res;
    } catch (e) {
        log.error('校徽报警保安队长分配任务异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});

/**
 * 校徽报警 - 存某个组织的队长分配任务已经完成存数据细节
 */
router.post('/saveInsOfGearTask', async (ctx, next) => {
    try {
        let res = studentService.saveInsOfGearTask(ctx, next);
        return await res;
    } catch (e) {
        log.error('校徽报警保安队长执行分配任务异常', e);
        ctx.body = result.go(result.code.CODE_500);
        return;
    }
});


router.post('/test', async (ctx, next) => {
    //圈内
    let a = `{"Report-Count":"24","Gsensor-Count":"0","Send-Length":"76","POST":" /mobile HTTP/1.1","Send-Type":"T166","Start-Count":"26","Sos-Sign":"1","Wifi-Search-Count":"6","studentRelation":{"schoolList":[{"id":1,"name":"经纬大道小学001","abbreviation":"1小","address":"九龙坡经纬大道021","tel":"023-1000140","orgCode":500107001,"longitude":0.0,"latitude":0.0,"siEcId":1,"createDate":"2019-10-24 16:08:00","updateDate":"2019-10-24 16:08:00","dataStatus":1,"introduction":"新建学校刚开业"}],"teacherList":[{"id":1,"name":"赖加成","siId":12,"tel":18944444444,"sex":1,"age":24,"entryTime":"2019-10-29 15:44:50","seniority":1,"tncumbency":0,"createDate":"2019-10-24 16:26:13","updateDate":"2019-10-24 16:26:13","dataStatus":1}],"parentList":[{"id":1,"parentName":"梅塞雷斯","parentSex":1,"parentAge":30,"parentTel":18944444444,"createDate":"2019-10-24 16:31:52","updateDate":"2019-10-24 16:31:52","dataStatus":1}],"classList":[{"id":1,"siOrgCode":500107001,"name":"一年级二班","classNum":"二班","boundary":"20190124","gradeName":"一年级","dataStatus":1,"createDate":"2019-10-24 16:11:57","updateDate":"2019-10-24 16:11:57","graduateStatus":1,"classSerialNumber":10000000001}]},"Connection":"keep-alive","Nb-Strength":"29","Server-id":"3118799","Serial-Number":"201802071033146602","Host":" www.hejiayuannet.com","Gps":[{"Latitude":"23.155678","Longitude":"113.330908"}],"Student":{"id":6,"studentName":"小红","studentSex":2,"studentAge":7,"studentTel":0,"createDate":"2019-10-24 16:22:41","updateDate":"2019-10-24 16:22:41","dataStatus":1,"idCardNo":"2525120101020201","studentNumber":"001","imeiNo":869075030123618},"app-version":"20191105_22:03:57__gprs_y2_b33","IMSI-id":"9460001691932462","Parameter-Label":"-1","Electricity":"4257","IMEI-id":"869075030123618","Content-Length":"124","Wifi":{"Wifi-Time":"20180207103314","Wifi-Content":[{"Wifi-Mac":"34:cd:6d:f7:c0:30"}]},"Step-Count":"328"}
    `;
    //圈外
    let b = `{"Report-Count":"24","Gsensor-Count":"0","Send-Length":"76","POST":" /mobile HTTP/1.1","Send-Type":"T166","Start-Count":"26","Sos-Sign":"1","Wifi-Search-Count":"6","studentRelation":{"schoolList":[{"id":1,"name":"经纬大道小学001","abbreviation":"1小","address":"九龙坡经纬大道021","tel":"023-1000140","orgCode":500107001,"longitude":0.0,"latitude":0.0,"siEcId":1,"createDate":"2019-10-24 16:08:00","updateDate":"2019-10-24 16:08:00","dataStatus":1,"introduction":"新建学校刚开业"}],"teacherList":[{"id":1,"name":"赖加成","siId":12,"tel":18944444444,"sex":1,"age":24,"entryTime":"2019-10-29 15:44:50","seniority":1,"tncumbency":0,"createDate":"2019-10-24 16:26:13","updateDate":"2019-10-24 16:26:13","dataStatus":1}],"parentList":[{"id":1,"parentName":"梅塞雷斯","parentSex":1,"parentAge":30,"parentTel":18944444444,"createDate":"2019-10-24 16:31:52","updateDate":"2019-10-24 16:31:52","dataStatus":1}],"classList":[{"id":1,"siOrgCode":500107001,"name":"一年级二班","classNum":"二班","boundary":"20190124","gradeName":"一年级","dataStatus":1,"createDate":"2019-10-24 16:11:57","updateDate":"2019-10-24 16:11:57","graduateStatus":1,"classSerialNumber":10000000001}]},"Connection":"keep-alive","Nb-Strength":"29","Server-id":"3118799","Serial-Number":"201802071033146602","Host":" www.hejiayuannet.com","Gps":[{"Latitude":"23.137335","Longitude":"113.33831"}],"Student":{"id":6,"studentName":"小红","studentSex":2,"studentAge":7,"studentTel":0,"createDate":"2019-10-24 16:22:41","updateDate":"2019-10-24 16:22:41","dataStatus":1,"idCardNo":"2525120101020201","studentNumber":"001","imeiNo":869075030123618},"app-version":"20191105_22:03:57__gprs_y2_b33","IMSI-id":"9460001691932462","Parameter-Label":"-1","Electricity":"4257","IMEI-id":"869075030123618","Content-Length":"124","Wifi":{"Wifi-Time":"20180207103314","Wifi-Content":[{"Wifi-Mac":"34:cd:6d:f7:c0:30"}]},"Step-Count":"328"}
    `;
    studentService.locationSave(b);
    ctx.body = result.go(result.code.CODE_500);
    return;
});


module.exports = router
